package org.jeecg.modules.demo.stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.JstlHttpUpload;
import org.jeecg.modules.demo.stock.entity.TbStockK;
import org.jeecg.modules.demo.stock.entity.TbStockMinute;
import org.jeecg.modules.demo.stock.entity.TbStockMinuteVo;
import org.jeecg.modules.demo.stock.mapper.TbStockMinuteMapper;
import org.jeecg.modules.demo.stock.service.ITbStockKService;
import org.jeecg.modules.demo.stock.service.ITbStockMinuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 股票分交易记录
 * @Author: jeecg-boot
 * @Date: 2022-02-15
 * @Version: V1.0
 */
@Service
public class TbStockMinuteServiceImpl extends ServiceImpl<TbStockMinuteMapper, TbStockMinute> implements ITbStockMinuteService {

    @Autowired
    private ITbStockKService tbStockKService;
    //发送请求方法
    @Autowired
    private JstlHttpUpload jstlHttpUpload;


    @Autowired
    private TbStockMinuteMapper tbStockMinuteMapper;

    @Override
    public void saveList(String codeS) throws ParseException {
        //进行数据处理
        boolean flag = codeS.substring(0, 1).equals("6");
        String url = "";
        //判断是不是 600905
        if (flag) {
            //是
            url = "http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=fa5fd1943c7b386f172d6893dbfba10b&ndays=5&iscr=0&secid=1." + codeS + "&cb=jQuery11240004309865361846743_1637590099769&_=1637590099931";

        } else {
            //不是
            url = "http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=fa5fd1943c7b386f172d6893dbfba10b&ndays=5&iscr=0&secid=0." + codeS + "&cb=jQuery112408843913876222274_1637667605625&_=1637667605713";

        }
        String a = jstlHttpUpload.setGetHangfei(url, null, null);
        int index = a.indexOf("(");//获取第一个"("的位置
        String str1 = a.substring(index + 1);//不包含"("
        String str = str1.substring(0, str1.length() - 2);


        JSONObject jsonObject = JSON.parseObject(str);
        String data = jsonObject.getString("data");
        JSONObject jsonData = JSON.parseObject(data);
        String code = jsonData.getString("code");
        JSONArray trends = jsonData.getJSONArray("trends");
        List<TbStockMinute> list = new ArrayList<>();
        //获取全部的日数据

        QueryWrapper<TbStockK> qu = new QueryWrapper<>();
        qu.eq("stock_id", code);
        List<TbStockK> timeList = tbStockKService.list(qu);

        Map<String, String> map = new HashMap<>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        for (TbStockK tbStockK : timeList) {
            Date dateTime = tbStockK.getDateTime();
            String format = sdf1.format(dateTime);
            String id = tbStockK.getId();
            map.put(format, id);
        }
        for (int i = 0; i < trends.size(); i++) {
            String trend = trends.getString(i);
            String[] split = trend.split(",");
            //2021-11-05 14:47,51.50,51.43,51.53,51.43,3472,17870158.00,51.714
            String dayTime = split[0];//时间
            String last = split[1];//上次值
            String value1 = split[2];//1
            String value2 = split[3];//2
            String value3 = split[4];//3
            String turnover = split[5];//成交量
            String type = split[6];//字段一
            String mean = split[7];//平均值

            TbStockMinute bean = new TbStockMinute();
            bean.setId(code + "T" + dayTime);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date parse = sdf.parse(dayTime);

            String format = sdf1.format(parse);
            String value = map.get(format);
            bean.setTimeId(value);//历史id
            bean.setDayTime(parse);
            bean.setLast(Double.parseDouble(last));

            bean.setValue1(Double.parseDouble(value1));
            bean.setValue2(Double.parseDouble(value2));
            bean.setValue3(Double.parseDouble(value3));

            bean.setTurnover(Double.parseDouble(turnover));
            bean.setType(Double.parseDouble(type));
            bean.setMean(Double.parseDouble(mean));

            bean.setRemark(trend);
            bean.setCode(code);
            list.add(bean);
        }
        this.saveOrUpdateBatch(list);

    }

    @Override
    public List<TbStockMinuteVo> selectList(String code,String order) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        List<String> list = tbStockMinuteMapper.selectGroup(code);
        List<TbStockMinuteVo> result = new ArrayList<>();
        if (list != null && !list.isEmpty()) {

            for (String timeId : list) {
                QueryWrapper<TbStockMinute> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("time_id", timeId);
                if ("asc".equals(order)){
                    queryWrapper.orderByAsc("value1");
                }else{
                    queryWrapper.orderByDesc("value1");
                }
                queryWrapper.last("limit 2");
                List<TbStockMinute> tbStockMinutes = tbStockMinuteMapper.selectList(queryWrapper);
                //进行转换
                List<TbStockMinuteVo> voList = tbStockMinutes.stream().map(tbStockMinute -> {
                    TbStockMinuteVo vo = new TbStockMinuteVo();
                    vo.setTimeId(tbStockMinute.getTimeId());
                    Date dayTime = tbStockMinute.getDayTime();
                    String format1 = format.format(dayTime);
                    vo.setDayTime(format1);
                    vo.setValue1(tbStockMinute.getValue1());
                    return vo;
                }).collect(Collectors.toList());

                result.addAll(voList);


            }

        }
        return result;
    }
}
